package com.example.reward;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.reward.fragments.CalculatorFragment;
import com.example.reward.fragments.HistoryFragment;
import com.example.reward.fragments.HomeFragment;
import com.example.reward.fragments.ProfileFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;
    private final Fragment[] fragments = new Fragment[] {
            new HomeFragment(), new CalculatorFragment(),
            new HistoryFragment(), new ProfileFragment()
    };
    private int currentFragmentIndex = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ✅ 关闭系统默认 Insets 处理
        WindowCompat.setDecorFitsSystemWindows(getWindow(), false);

        // ✅ 状态栏透明
        getWindow().setStatusBarColor(Color.TRANSPARENT);

        // ✅ 状态栏图标变暗色（如果背景是浅色）
        WindowInsetsControllerCompat controller = new WindowInsetsControllerCompat(getWindow(), getWindow().getDecorView());
        controller.setAppearanceLightStatusBars(true);

        // ✅ 动态处理导航栏和底部间距
        View navHost = findViewById(R.id.nav_host_fragment);
        bottomNavigationView = findViewById(R.id.bottom_nav);

        // 等 BottomNavigationView 完成测量后再设置 padding
        bottomNavigationView.post(() -> {
            int bottomNavHeight = bottomNavigationView.getHeight();

            ViewCompat.setOnApplyWindowInsetsListener(navHost, (v, insets) -> {
                Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                // 顶部留状态栏，底部留导航栏 + BottomNavigationView 高度
                v.setPadding(
                        0,
                        systemBars.top,
                        0,
                        systemBars.bottom + bottomNavHeight
                );
                return insets;
            });

            // 触发一次 Insets 更新
            ViewCompat.requestApplyInsets(navHost);
        });

        // ✅ Fragment 切换逻辑保持不变
        bottomNavigationView.setOnItemSelectedListener(item -> {
            View view = bottomNavigationView.findViewById(item.getItemId());
            view.animate().scaleX(1.2f).scaleY(1.2f).setDuration(150)
                    .withEndAction(() -> view.animate().scaleX(1f).scaleY(1f).setDuration(150).start())
                    .start();

            int itemId = item.getItemId();
            if (itemId == R.id.nav_home) {
                switchFragment(0);
                return true;
            } else if (itemId == R.id.nav_calculator) {
                switchFragment(1);
                return true;
            } else if (itemId == R.id.nav_history) {
                switchFragment(2);
                return true;
            } else if (itemId == R.id.nav_profile) {
                switchFragment(3);
                return true;
            }
            return false;
        });

        switchFragment(0); // 默认显示首页
    }


    public void switchFragment(int index) {
        if (currentFragmentIndex == index) return;

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        // 添加自定义动画：左右滑动 + 淡入淡出
        ft.setCustomAnimations(
                R.anim.slide_in_right,   // 新 Fragment 进入
                R.anim.slide_out_left,   // 当前 Fragment 离开
                R.anim.slide_in_left,    // 返回时新 Fragment 进入
                R.anim.slide_out_right   // 返回时当前 Fragment 离开
        );

        ft.replace(R.id.nav_host_fragment, fragments[index]);
        ft.commit();
        currentFragmentIndex = index;
    }

    public void switchFragmentAndUpdateTab(int index) {
        switchFragment(index);

        // 让底部导航栏同步高亮
        int menuItemId;
        switch (index) {
            case 0:
                menuItemId = R.id.nav_home;
                break;
            case 1:
                menuItemId = R.id.nav_calculator;
                break;
            case 2:
                menuItemId = R.id.nav_history;
                break;
            case 3:
                menuItemId = R.id.nav_profile;
                break;
            default:
                menuItemId = R.id.nav_home;
        }

        bottomNavigationView.setSelectedItemId(menuItemId);
    }

}
